package com.soul.base.pattern.strategy;

import java.util.HashMap;
import java.util.Map;

/**
 * 策略环境，输出水果价格
 */
public class FruitPrice {

    public static final String APPLE = "apple";
    public static final String BANANA = "banana";

    /**
     * 如果与spring结合, 可以通过注解或包扫描方式来自动生成map,
     * APPLE, BANANA常量放入对应策略类中
     */
    private static Map<String, Fruit> fruits = new HashMap<String, Fruit>() {{
        put(APPLE, new Apple());
        put(BANANA, new Banana());
    }};

    /**
     * 策略对象
     */
    private Fruit fruit;

    public FruitPrice() {
    }

    /**
     * @param fruit 策略对象
     */
    public FruitPrice(Fruit fruit) {
        this.fruit = fruit;
    }

    /**
     * 输出水果价格
     */
    public void printFruitPrice(String type) {
        fruits.get(type).price();
    }

    /**
     * 输出水果价格
     */
    public void printFruitPrice() {
        fruit.price();
    }

}